package com.huarui.sale.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.ewaf.framework.dao.impl.BaseDaoImpl;
import org.springframework.orm.ibatis.SqlMapClientCallback;

import com.huarui.sale.dao.IExpressDao;
import com.huarui.sale.model.Express;
import com.ibatis.sqlmap.client.SqlMapExecutor;

public class ExpressDao extends BaseDaoImpl implements IExpressDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Express> getExpressBySaleId(Integer saleId) {

		return getSqlMapClientTemplate().queryForList(namespace + ".getExpressBySaleId", saleId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Integer> getExpressIdsBySaleId(Integer saleId) {
		
		return getSqlMapClientTemplate().queryForList(namespace+".getExpressIdsBySaleId", saleId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getExpressForMapBySaleId(Integer saleId) {
		return getSqlMapClientTemplate().queryForList(namespace+".getExpressForMapBySaleId", saleId);
	}

	@Override
	public void deleteExpressBySaleId(Integer id) {
	   getSqlMapClientTemplate().delete(namespace+".deleteExpressBySaleId", id);
		
	}

	@Override
	public void batchUpdateExpress(final List<Express> expresses) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				executor.startBatch();
				int batch = 0;
				for(Express express : expresses){
					executor.update(namespace+".update", express);
					batch++;
					if (batch == 50) {
						executor.executeBatch();
						batch = 0;
					}
				}
				executor.executeBatch();
				return null;
			}
		});
	}

	@Override
	public Integer batchDeleteExpress(Map<String, Object> map) {
		return getSqlMapClientTemplate().delete(namespace+".deleteByIds", map);
		
	}

}
